const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

// 解决跨域问题
const cors = require('cors')

// 导入路由文件

// 小程序端路由文件
const CarouselApiRouter = require('./routes/web/carousel'); //轮播图路由
const RegionApiRouter = require('./routes/web/region')  //省份和城市路由
const SceneryApiRouter = require('./routes/web/scenery')  //景点路由
const HotelApiRouter = require('./routes/web/hotel')  //酒店路由
const RestaurantApiRouter = require('./routes/web/restaurant')  //餐厅路由
const CityApiRouter = require('./routes/web/city')  //城市路由
const PostAipRouter = require('./routes/web/post')  //攻略路由
const SearchApiRouter = require('./routes/web/search')  //搜索路由
const CollectionApiRouter = require('./routes/web/collection')  //收藏路由
const AIApiRouter = require('./routes/web/ai')  //AI路由
const AuthApiRouter = require('./routes/web/auth')  //用户注册登录路由
const UserInfoApiRouter = require('./routes/web/user')  //用户个人信息登录路由
const OrderApiRouter = require('./routes/web/order')  //订单路由
const EvaluateApiRouter = require('./routes/web/evaluate')  //评价路由

// 后台管理系统路由文件
const AdminUserApiRouter = require('./routes/apis/auth')  //管理员登录
const AdminInfoApiRouter = require('./routes/apis/admin') //管理员信息路由
const AdminSceneryApiRouter = require('./routes/apis/scenery')  //景点路由
const AdminHotelApiRouter = require('./routes/apis/hotel')  //酒店路由
const AdminRestaurantApiRouter = require('./routes/apis/restaurant')  //餐厅路由
const AdminPostAipRouter = require('./routes/apis/post')  //攻略路由
const AdminUserAipRouter = require('./routes/apis/user')  //用户路由
const AdminChartApiRouter = require('./routes/apis/chart')  //图表路由
const AdminOrderApiRouter = require('./routes/apis/order')  //订单路由
const AdminEvaluateApiRouter = require('./routes/apis/evaluate')  //评价路由



const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 解决跨域问题
app.use(cors())

// 使用路由

// 小程序路由
app.use('/', CarouselApiRouter)
app.use('/', RegionApiRouter)
app.use('/', SceneryApiRouter)
app.use('/', HotelApiRouter)
app.use('/', RestaurantApiRouter)
app.use('/', CityApiRouter)
app.use('/', PostAipRouter)
app.use('/', SearchApiRouter)
app.use('/', CollectionApiRouter)
app.use('/', AIApiRouter)
app.use('/', AuthApiRouter)
app.use('/', UserInfoApiRouter)
app.use('/', OrderApiRouter)
app.use('/', EvaluateApiRouter)

// 后台管理系统路由
app.use('/api', AdminUserApiRouter)
app.use('/api', AdminInfoApiRouter)
app.use('/api', AdminSceneryApiRouter)
app.use('/api', AdminHotelApiRouter)
app.use('/api', AdminRestaurantApiRouter)
app.use('/api', AdminPostAipRouter)
app.use('/api', AdminUserAipRouter)
app.use('/api', AdminChartApiRouter)
app.use('/api', AdminOrderApiRouter)
app.use('/api', AdminEvaluateApiRouter)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
